package com.controller.back;

import com.service.AdminService;
import com.util.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("back")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("login")
    public String login(String username, String password, HttpServletResponse res, String VCode, HttpSession session){
        String realCode = (String) session.getAttribute("realCode");
        if (VCode != null && !VCode.equalsIgnoreCase(realCode)) {
            //如果验证码不正确，重定向到登录页面
            return "redirect:/back/login.jsp";
        }
        Cookie c = new Cookie("name",username);
        res.addCookie(c);
        boolean login = adminService.login(username, password);
        if(login){
            session.setAttribute("login",true);
            return "redirect:/back/manager.jsp";
        }else{
            return "redirect:/back/login.jsp";
        }
    }
    @RequestMapping("register")
    public String register(String name, String password,HttpServletResponse res){
        boolean register = adminService.register(name, password);
        if(register){
            return "redirect:/back/login";
        }else{
            return "redirect:/register";
        }
    }
}
